﻿/*
----------------------------------
    Rodeo Game Engine

    Rodeo IO Module
    File3DS.h - header file

    Copyright(c) Marcin Goryca
----------------------------------
*/
#ifndef RODEO_IO_FILE3DS_H_
#define RODEO_IO_FILE3DS_H_

#ifndef RODEO_CORE_ERROR_H_
#include "core\error.h"
#endif

#ifndef RODEO_IO_IMPORTER_H_
#include "io\importer.h"
#endif

#ifndef RODEO_IO_LOG_H_
#include "io\log.h"
#endif

#ifndef RODEO_MATH_MATRIX4_H_
#include "math\matrix4.h"
#endif

#ifndef RODEO_MATH_VECTOR2_H_
#include "math\vector2.h"
#endif

#ifndef RODEO_RENDERER_COLORS_H_
#include "renderer\colors.h"
#endif

namespace rodeoio
{
// ---- < Chunk > ---- //
struct Chunk
{
    rodeocore::us id_;
    rodeocore::ui length_; 
    rodeocore::ui bytesread_;
};

class File3DS : public Importer
{
public:
    File3DS()
        :file_(NULL),
         classname_("File3DS")
    {}
    virtual ~File3DS(){}

    virtual bool import(const char* filename);

    virtual const std::string& getClassName()const { return classname_; }

private:
    bool release();

    void readHeaderChunk(Chunk& chunk);
    void readVersionChunk(Chunk& chunk);
    void readChunk(Chunk& chunk);
    rodeocore::ui skipChunk(Chunk& chunk);
    void readNextChunk(Chunk& chunk);

    void readMeshChunk(rodeoentity::Mesh& m, Chunk& c);
    void readMaterialChunk(rodeoentity::MeshMaterial& mat, Chunk& c);

    int getString(char* str);

    void readVertexList(rodeoentity::Mesh& m, Chunk& c);
    void readFaceList(rodeoentity::Mesh& m, Chunk& c);
    void readUVMapping(rodeoentity::Mesh& m, Chunk& c);

    void readFaceMaterial(rodeoentity::Mesh& m, Chunk& chunk);
    void readColorChunk(rodeoentity::MeshMaterial& mat, Chunk& c);

//==== MEMBERS ====//
    // Maximum number of chars in the buffer
    static const int BUFFER_MAX = 500000;
    HANDLE file_;
    DWORD bytesread_;
    char buffer_[BUFFER_MAX];
    std::string classname_;
};
}    //end of rodeoio namespace
#endif    //RODEO_IO_FILE3DS_H_